//
//  MenuShiLiaoViewController.m
//  Products_B
//
//  Created by lanou on 16/6/7.
//  Copyright © 2016年 朋学友. All rights reserved.
//
//  http://api.izhangchu.com/
//  左边   department_id=3&methodName=DiseaseTree&token=&user_id=&version=4.4
//  右边   department_id=3&disease_id=18&methodName=DiseaseSearch&page=1&size=6&token=&user_id=&version=4.4
#import "MenuShiLiaoViewController.h"
#import "MenuShiliaoLeftModel.h"
#import "MenuShiliaoCollectionViewCell.h"
#import "MenuFirstDetailViewController.h"
#import "MenuShiliaoRightModel.h"
#import "MenuShiLiaoTableViewCell.h"

@interface MenuShiLiaoViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>


@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *leftModelArray;

@property (nonatomic,strong)NSMutableArray *rightModelArray;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableDictionary *parDic;


@property (nonatomic, strong)NSString *disease_id;
@property (nonatomic, strong)NSString *page;
@end

@implementation MenuShiLiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.disease_id = @"18";
    self.page = @"1";
    self.parDic = @{@"department_id":self.department_id,@"disease_id":self.disease_id,@"methodName":@"DiseaseSearch",@"page":self.page,@"size":@"6",@"token":@"",@"user_id":@"",@"version":@"4.4"}.mutableCopy;
    self.rightModelArray = [NSMutableArray array];
   
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth / 5 * 2, kHeight - 64) collectionViewLayout:flowlayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[MenuShiliaoCollectionViewCell class] forCellWithReuseIdentifier:@"MenuShiliaoCollectionViewCell"];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(kWidth/5*2, 0, kWidth/5*3, kHeight - 60) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 110;
    [self.tableView registerClass:[MenuShiLiaoTableViewCell class] forCellReuseIdentifier:@"MenuShiLiaoTableViewCell"];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page = [NSString stringWithFormat:@"%ld",[self.page integerValue] + 1];
        self.parDic[@"page"] = self.page;
        NSLog(@"%@",self.page);
        [self requestRightData];
    }];
    [self.view addSubview:_tableView];
    [self.view addSubview:_collectionView];
    [self requestleftData];
    [self requestRightData];
    
    
    UILabel *lable = [[UILabel alloc]init];
    
    
    lable.adjustsFontSizeToFitWidth = YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.rightModelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuShiLiaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MenuShiLiaoTableViewCell" forIndexPath:indexPath];
    MenuShiliaoRightModel *model = self.rightModelArray[indexPath.row];
    cell.model = model;
    
    [cell.playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.playButton.tag  = 510 + indexPath.row;
    return cell;
}


- (void)requestRightData{
    
    
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:self.parDic finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *array = [MenuShiliaoRightModel modelConfigureWithDic:jsonDic];
        for (MenuShiliaoRightModel *model in array) {
            [self.rightModelArray addObject:model];
        }
        
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } error:nil];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.leftModelArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return  CGSizeMake(kWidth/5*2, 70);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MenuShiliaoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuShiliaoCollectionViewCell" forIndexPath:indexPath];

    MenuShiliaoLeftModel *model = self.leftModelArray[indexPath.row];
 
    if (model.isSelect == YES) {
        cell.myView.backgroundColor = [UIColor redColor];
    }else{
        cell.myView.backgroundColor = [UIColor grayColor];
    }
    
    cell.model = model;
    
    
    
    return cell;
}



- (void)requestleftData{
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:@{@"department_id":self.department_id,@"methodName":@"DiseaseTree",@"token":@"",@"user_id":@"",@"version":@"4.4"} finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.leftModelArray = [MenuShiliaoLeftModel modelConfigureWithDic:jsonDic];
        NSLog(@"%@",self.leftModelArray);
        MenuShiliaoLeftModel *model = self.leftModelArray[0];
        model.isSelect = YES;
        [self.collectionView reloadData];
    } error:nil];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
        for (MenuShiliaoLeftModel *model in self.leftModelArray) {
            model.isSelect = NO;
        }
    
    for (MenuShiliaoCollectionViewCell *cell in collectionView.visibleCells) {
        cell.myView.backgroundColor = [UIColor grayColor];
    }
    MenuShiliaoLeftModel *model = self.leftModelArray[indexPath.row];
    MenuShiliaoCollectionViewCell *cell = (MenuShiliaoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.myView.backgroundColor = [UIColor redColor];
    model.isSelect = YES;
    
    
    
    self.disease_id = model.id;
    self.parDic[@"disease_id"] = self.disease_id;
    self.page = @"1";
    self.parDic[@"page"] = self.page;
    [self.rightModelArray removeAllObjects];
    [self requestRightData];
    
    
//    [self.collectionView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuFirstDetailViewController *Vc = [[MenuFirstDetailViewController alloc]init];
    MenuShiliaoRightModel *model = self.rightModelArray[indexPath.row];
    Vc.dishes_id = model.dishes_id;
    [self.navigationController pushViewController:Vc animated:YES];
}
#pragma mark - 当push的时候隐藏tabBar
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
#pragma mark - 播放视频 - 
- (void)playButtonAction:(UIButton *)button{
    NSMutableArray *array = [NSMutableArray array];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    MenuShiliaoRightModel *model = self.rightModelArray[button.tag - 510];
    NSLog(@"%@  %@",model.title,model.video);
    
    [dic setValue:model.title forKey:@"title"];
    [dic setValue:model.video forKey:@"url"];
    [array addObject:dic];
   VedioPlayerViewController *Vc = [[VedioPlayerViewController alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 164) array:array index:0];
    [self.navigationController pushViewController:Vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
